package com.huawei.hwid.api.common;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import com.huawei.cloudservice.CloudAccountManager;
import com.huawei.cloudservice.CloudRequestHandler;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.huawei.hwid.core.d.b.e;
import com.huawei.hwid.core.d.l;
import com.huawei.hwid.core.d.o;

public class g extends Builder {
    private CloudRequestHandler a;
    private String b;
    private Context c;
    private AlertDialog d;
    private String e;
    private String f;

    public g(Activity activity, String str, final Bundle bundle, CloudRequestHandler cloudRequestHandler) {
        super(activity, o.a(activity));
        this.c = activity;
        this.a = cloudRequestHandler;
        this.b = str;
        if (bundle != null) {
            this.e = bundle.getString("serviceToken");
            this.f = bundle.getString(CloudAccountManager.KEY_APP_ID_WEB);
        }
        setNegativeButton(l.a(this.c, "account_cancel"), new OnClickListener(this) {
            final /* synthetic */ g a;

            {
                this.a = r1;
            }

            public void onClick(DialogInterface dialogInterface, int i) {
                e.b("CloudAccountManagerDialog", "account_cancel", true);
                this.a.b();
            }
        });
        View inflate = View.inflate(this.c, l.c(this.c, "cs_listview_item_more_account"), null);
        inflate.findViewById(l.d(this.c, "id_account_manager")).setOnClickListener(new View.OnClickListener(this) {
            final /* synthetic */ g b;

            public void onClick(View view) {
                e.b("CloudAccountManagerDialog", "account_manager", true);
                if (e.a(this.b.c, this.b.b)) {
                    this.b.a(this.b.c);
                } else {
                    this.b.a(this.b.c, bundle);
                }
                this.b.b();
            }
        });
        inflate.findViewById(l.d(this.c, "id_logout")).setOnClickListener(new View.OnClickListener(this) {
            final /* synthetic */ g a;

            {
                this.a = r1;
            }

            public void onClick(View view) {
                e.b("CloudAccountManagerDialog", "account_logout", true);
                this.a.a();
                this.a.b();
            }
        });
        setView(inflate);
    }

    public void a(Context context) {
        try {
            Intent intent = new Intent();
            intent.setAction("com.huawei.hwid.ACTION_MAIN_SETTINGS");
            intent.setPackage("com.huawei.hwid");
            intent.putExtra(HwAccountConstants.EXTRA_IS_GOTO_WELCOME, true);
            context.startActivity(intent);
        } catch (RuntimeException e) {
            e.d("CloudAccountManagerDialog", "RuntimeException22", true);
        } catch (Exception e2) {
            e.d("CloudAccountManagerDialog", "Exception", true);
        }
    }

    public void a(Context context, Bundle bundle) {
        e.b("CloudAccountManagerDialog", "startCloudAccountCenter", true);
        try {
            Intent intent = new Intent(context, CloudAccountCenterActivity.class);
            intent.putExtra(CloudAccountManager.KEY_SERVICE_TOKEN_WEB, this.e);
            intent.putExtra(CloudAccountManager.KEY_APP_ID_WEB, this.f);
            intent.putExtra(CloudAccountManager.KEY_USER_ACCOUNT_WEB, this.b);
            intent.putExtras(bundle);
            if (context != null) {
                intent.setPackage(context.getPackageName());
                context.startActivity(intent);
            }
        } catch (RuntimeException e) {
            e.d("CloudAccountManagerDialog", "RuntimeException", true);
        } catch (Exception e2) {
            e.d("CloudAccountManagerDialog", "Exception", true);
        }
    }

    public void a() {
        Bundle bundle = new Bundle();
        bundle.putInt(CloudAccountManager.KEY_RESULT_CODE, 1);
        this.a.onFinish(bundle);
    }

    public void b() {
        if (this.d != null && this.d.isShowing()) {
            this.d.dismiss();
        }
    }

    public AlertDialog show() {
        this.d = super.show();
        return this.d;
    }
}
